package com.iscreen.core.utils.jms;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.iscreen.utils.exceptions.IScreenException;
import com.iscreen.utils.exceptions.IScreenExceptionCodes;

public class JMSUtils
{
	private final static String JMS_CONNECTION_FACTORY = "ConnectionFactory";
	
	public static void send(String queueName, Serializable message) throws IScreenException
	{
		try
		{
			InitialContext ctx = new InitialContext();
			Queue queue = (Queue) ctx.lookup(queueName);
			ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(JMS_CONNECTION_FACTORY);
			Connection connection = connectionFactory.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(queue);
			Message objectMessage = session.createObjectMessage(message);
			producer.send(objectMessage);
		}
		catch (NamingException e)
		{
			throw new IScreenException(IScreenExceptionCodes.JMS_SEND_ERROR, e);
		}
		catch (JMSException e)
		{
			throw new IScreenException(IScreenExceptionCodes.JMS_SEND_ERROR, e);
		}
	}

	
}
